package cn.darkhorse.ticket.basis.service.impl;

import cn.darkhorse.ticket.basis.mapper.TicketDefectProductInfoMapper;
import cn.darkhorse.ticket.basis.pojo.TicketDefectProductInfo;
import cn.darkhorse.ticket.basis.pojo.TicketProcedure;
import cn.darkhorse.ticket.basis.service.TicketDefectProductInfoService;
import cn.darkhorse.ticket.basis.service.TicketProcedureService;
import cn.darkhorse.ticket.code.RepeatCheck;
import cn.darkhorse.ticket.constants.TicketConstants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 不良品信息 服务实现类
 * </p>
 *
 * @author maoguanjun
 * @since 2024-01-10
 */
@Service
public class TicketDefectProductInfoServiceImpl extends ServiceImpl<TicketDefectProductInfoMapper, TicketDefectProductInfo> implements TicketDefectProductInfoService {

    @Resource
    private RepeatCheck<TicketDefectProductInfo> repeatCheck;
    @Resource
    private TicketProcedureService ticketProcedureService;
    /**
     * 列表
     *
     * @param dataName
     * @param dataCode
     * @return
     */
    @Override
    public List<TicketDefectProductInfo> listAll(String dataName, String dataCode) {
        LambdaQueryWrapper<TicketDefectProductInfo> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dataName)){
            wrapper.like(TicketDefectProductInfo::getName,dataName);
        }
        if(StringUtils.isNotBlank(dataCode)){
            wrapper.like(TicketDefectProductInfo::getCode,dataCode);
        }
        wrapper.orderByDesc(TicketDefectProductInfo::getGmtCreate);
        return list(wrapper);
    }

    /**
     * 查看
     *
     * @param dataId
     * @return
     */
    @Override
    public TicketDefectProductInfo view(Long dataId) {
        return getById(dataId);
    }

    /**
     * 保存
     *
     * @param info
     * @return
     */
    @Override
    public boolean saveData(TicketDefectProductInfo info) {
        //TODO 编码校验
        repeatCheck.repeat(this,info, TicketConstants.BUSINESS_TYPE_BLP);
        LambdaQueryWrapper<TicketDefectProductInfo>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(TicketDefectProductInfo::getName,info.getName());
        if(null!=info.getId()){
            //TODO 修改校验
            if(isQuote(info.getId())){
                throw new ServiceException("该不良品项已被工序引用，不允许修改!");
            }
            wrapper.ne(TicketDefectProductInfo::getId,info.getId());
        }
        long count = count(wrapper);
        if(count>0){
            throw new ServiceException("该不良品项名称系统已存在！");
        }
        return saveOrUpdate(info);
    }

    /**
     * 删除
     *
     * @param dataId
     * @return
     */
    @Override
    public boolean delete(Long dataId) {
        //TODO 删除校验
        if(isQuote(dataId)){
            throw new ServiceException("该不良品项已被工序引用，不允许删除!");
        }
        return removeById(dataId);
    }

    /**
     * 是否被引用
     * @param dataId
     * @return
     */
    @Override
    public boolean isQuote(Long dataId){
        LambdaQueryWrapper<TicketProcedure> wrapper=new LambdaQueryWrapper<>();
        wrapper.apply(dataId!=null ,"FIND_IN_SET ('" + dataId + "',defective_items)");
        long count = ticketProcedureService.count(wrapper);
        if(count>0){
            return true;
        }
        return false;
    }
}
